<script setup lang="ts">
import { ref } from 'vue'
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'
import { Menu as IconMenu, SwitchButton, User } from '@element-plus/icons-vue'

const userStore = useUserStore()
const router = useRouter()

// 控制移动端抽屉菜单的显示
const mobileDrawerVisible = ref(false)

const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}
</script>

<template>
  <div>
    <el-menu
      :default-active="$route.path"
      class="el-menu-desktop"
      mode="horizontal"
      :ellipsis="false"
      router
    >
      <div class="logo-container">
        <div class="user-status-container">
          <template v-if="userStore.token">
            <el-dropdown>
              <span class="el-dropdown-link">
                <el-avatar shape="square" :size="40" :src="userStore.avatarUrl" />
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item :icon="User" @click="userStore.openProfileDrawer"
                    >修改信息</el-dropdown-item
                  >
                  <el-dropdown-item :icon="SwitchButton" divided @click="handleLogout"
                    >退出登录</el-dropdown-item
                  >
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
          <template v-else>
            <el-button type="primary" @click="router.push('/login')">登录 / 注册</el-button>
          </template>
        </div>
        <el-text v-if="userStore.token" class="username-text">{{ userStore.username }}</el-text>
      </div>

      <div class="flex-grow" />

      <el-menu-item index="/home">首页</el-menu-item>
      <el-menu-item index="/resume">个人简历</el-menu-item>
      <el-menu-item index="/projects">项目经历</el-menu-item>
      <el-menu-item index="/admin">后台管理</el-menu-item>
    </el-menu>

    <div class="el-menu-mobile">
      <div class="logo-container" @click="router.push('/home')">
        <el-avatar
          shape="square"
          :size="40"
          :fit="'fill'"
          :src="userStore.avatarUrl || '/assets/myLogo.jpg'"
        />
      </div>
      <el-icon class="hamburger-icon" :size="30" @click="mobileDrawerVisible = true">
        <IconMenu />
      </el-icon>
    </div>

    <el-drawer v-model="mobileDrawerVisible" title="导航" direction="rtl" size="40%">
      <el-menu :default-active="$route.path" @select="mobileDrawerVisible = false" router>
        <el-menu-item index="/home">首页</el-menu-item>
        <el-menu-item index="/resume">个人简历</el-menu-item>
        <el-menu-item index="/projects">项目经历</el-menu-item>
        <el-menu-item index="/admin">后台管理</el-menu-item>

        <el-divider />

        <template v-if="userStore.token">
          <el-menu-item @click="userStore.openProfileDrawer">修改信息</el-menu-item>
          <el-menu-item @click="handleLogout">退出登录</el-menu-item>
        </template>
        <template v-else>
          <el-menu-item @click="router.push('/login')">登录 / 注册</el-menu-item>
        </template>
      </el-menu>
    </el-drawer>
  </div>
</template>

<style scoped>
/* 默认样式 (桌面端) */
.el-menu-desktop {
  display: flex;
  height: 60px;
  align-items: center;
}
.el-menu-mobile {
  display: none; /* 移动端导航默认隐藏 */
}
.flex-grow {
  flex-grow: 1;
}
.logo-container {
  display: flex;
  align-items: center;
  cursor: pointer;
  margin-left: 20px;
}
.username-text {
  font-weight: bold;
}
.user-status-container {
  margin-right: 20px;
  display: flex;
  align-items: center;
}
.el-dropdown-link {
  cursor: pointer;
  display: flex;
  align-items: center;
}
.el-menu-item {
  font-size: 1.1em;
}
/* --- 移动端适配 --- */
@media (max-width: 768px) {
  .el-menu-desktop {
    display: none; /* 桌面端导航在小屏幕上隐藏 */
  }
  .el-menu-mobile {
    display: flex; /* 移动端导航在小屏幕上显示 */
    justify-content: space-between;
    align-items: center;
    height: 60px;
    padding: 0 20px;
    border-bottom: 1px solid var(--el-menu-border-color);
  }
  .hamburger-icon {
    cursor: pointer;
  }
  /* 抽屉内菜单样式 */
  .el-drawer .el-menu {
    border-right: none;
  }
}
</style>
